/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Rdf;

import NodeManager.Node;
import com.hp.hpl.jena.rdf.model.Bag;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import java.io.File;

/**
 *
 * @author Sean
 */
public class RdfWriter {

    public static void AddFile(Model model, File file, Node Node1, Node Node2) {
        if (model == null) {
            throw new IllegalArgumentException("Model cannot be null");
        }

        if (file == null) {
            throw new IllegalArgumentException("File cannot be null");
        }

        if (Node1 == null) {
            throw new IllegalArgumentException("Node1 cannot be null");
        }

        if (Node2 == null) {
            throw new IllegalArgumentException("Node2 cannot be null");
        }

        String myFileURI = Global.Data.BaseURI + file.getName();

        // create the resource
        //   and add the properties cascading style
        Resource myFile = model.createResource(myFileURI);

        myFile.addProperty(CreateProperty("Filename"), file.getName());
        myFile.addProperty(CreateProperty("LastModified"), String.valueOf(file.lastModified()));

        Resource myNodes = model.createResource(myFileURI + "/Nodes");
        myFile.addProperty(CreateProperty("Nodes"), myNodes);

        Bag NodesBag = model.createBag(myFileURI + "/Nodes");
        NodesBag.add(Node1.NodeName);
        NodesBag.add(Node2.NodeName);

    }

    public static Property CreateProperty(String resourceName) {
        Property prop = ResourceFactory.createProperty(Global.Data.BaseURI, resourceName);
        return prop;
    }

    public static Model BuildRdfModel(Node node) throws Exception {

        Model model = ModelFactory.createDefaultModel();

        // iterate through each file in _Location
        for (File file : node.GetLocation().listFiles()) {

            if (file.isDirectory()) {
                // TODO Ignore for now if it is a directory
                continue;
            }

            // TODO an error is thrown by the model when a "[" or "]" is contained in the filename
            //  we need to get a hash of the file to use as the URI
            String myFileURI = Global.Data.BaseURI + file.getName();

            // create the resource
            // and add the properties cascading style
            Resource myFile = model.createResource(myFileURI);
            myFile.addProperty(CreateProperty("NodeNumber"), node.NodeName);
            myFile.addProperty(CreateProperty("Filename"), file.getName());
            myFile.addProperty(CreateProperty("LastModified"), String.valueOf(file.lastModified()));
        }

        return model;

    }
}
